package com.good.shopping.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.good.shopping.product.dao.BrandDao;
import com.good.shopping.product.dao.CategoryDao;
import com.good.shopping.product.entity.BrandEntity;
import com.good.shopping.product.entity.CategoryEntity;
import com.good.shopping.product.service.BrandService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.good.common.utils.PageUtils;
import com.good.common.utils.Query;

import com.good.shopping.product.dao.CategoryBrandRelationDao;
import com.good.shopping.product.entity.CategoryBrandRelationEntity;
import com.good.shopping.product.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    // 品牌
    @Autowired
    BrandDao brandDao;

    // 商品三级分类
    @Autowired
    CategoryDao categoryDao;

    // 品牌分类关联
    @Autowired
    CategoryBrandRelationDao relationDao;

    @Autowired
    BrandService brandService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 保存
     * 保存关系表，由于关联表有冗余字段，保存的时候需要把 品牌名字、分类名字也保存
     */
    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        // 1 查询详细名字
        BrandEntity brandEntity = brandDao.selectById(categoryBrandRelation.getBrandId());
        CategoryEntity categoryEntity = categoryDao.selectById(categoryBrandRelation.getCatelogId());
        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());
        this.save(categoryBrandRelation);
    }


    // 根据id 修改某个字段
    @Override
    public void updateBrand(Long brandId, String name) {
        CategoryBrandRelationEntity relationEntity = new CategoryBrandRelationEntity();
        relationEntity.setBrandId(brandId);
        relationEntity.setBrandName(name);
        this.update(relationEntity,new UpdateWrapper<CategoryBrandRelationEntity>().eq("brand_id",brandId));
    }

    // 根据id 修改某个字段  - Mapping模式更新
    @Override
    public void updateCategory(Long catId, String name) {
        this.baseMapper.updateCatogory(catId,name);
    }

    // 根据三级分类id 查询全部的品牌
    @Override
    public List<BrandEntity> getBrandsByCatId(Long catId) {
        List<CategoryBrandRelationEntity> catelogId = this.list(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
        if (null != catelogId) {
            List<BrandEntity> collect = catelogId.stream().map(item -> {
                BrandEntity b = brandService.getById(item.getBrandId());
                return b;
            }).collect(Collectors.toList());
            return collect;
        }
        return null;
    }
}
